package com.zhongyu.item.provider.apiimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhongyu.comm.api.ItemApi;
import com.zhongyu.comm.vo.DmItem;
import com.zhongyu.item.provider.mapper.ItemMapper;
import com.zhongyu.item.provider.pojo.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ItemApiImpl implements ItemApi {

    @Resource
    private ItemMapper itemMapper;

    private  DmItem transfor(Item item){
        DmItem dmItem = new DmItem();
        BeanUtils.copyProperties(item,dmItem);
        return dmItem;
    }

    @Override
    public List<DmItem> findTopN(Integer ItemTypeId, Integer top) {
        QueryWrapper<Item> queryWrapper = Wrappers.query();
        queryWrapper.eq("item_type1_id",ItemTypeId)
                .orderByDesc("comment_count","avg_score","start_time")
        .last("limit "+top);
        List<Item>list =  itemMapper.selectList(queryWrapper);
        return list.stream().map(this::transfor).collect(Collectors.toList());
    }
}
